package com.liuzh.kaoyanspringboot.service.impl;

import cn.hutool.core.annotation.Alias;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liuzh.kaoyanspringboot.common.Constants;
import com.liuzh.kaoyanspringboot.entity.Files;
import com.liuzh.kaoyanspringboot.exception.ServiceException;
import com.liuzh.kaoyanspringboot.mapper.FileMapper;
import com.liuzh.kaoyanspringboot.service.FileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘志鸿
 * @since 2023-02-25
 */
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, Files> implements FileService {
    @Autowired
    private FileMapper fileMapper;

    @Override
    public Files getFileByMd5(String md5) {
        QueryWrapper<Files> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("md5",md5);
        List<Files> filesList = fileMapper.selectList(queryWrapper);
        return filesList.size() == 0 ? null : filesList.get(0);
    }

    @Override
    public boolean isEnableByUrl(String url) {
        QueryWrapper<Files> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url",url);
        List<Files> filesList = fileMapper.selectList(queryWrapper);
        Files file = filesList.size() == 0 ? null :filesList.get(0);
        if (file != null) {
            return file.getEnable();
        }
        return false;
    }
}
